package manipulation;

import static org.junit.Assert.*;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.File;
import java.net.URISyntaxException;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;


public class ManipulationExercise {
	static WebDriver driver= new FirefoxDriver(); 
	static FormElements formElementPage; 
	@Before
	public void setUp(){
		
		driver.get("http://www.compendiumdev.co.uk/selenium/basic_html_form.html");
	}
	@AfterClass
	public static void closeDriver(){
		driver.quit();
	}

	@Test
	public void testSubmitForm() {
		FormElements formElementPage = PageFactory.initElements(driver, FormElements.class);
		formElementPage.submitButton.click();
		assertFalse("Title is HTML Form Elements",driver.getTitle().equals("HTML Form Elements"));
	}
	@Test
	public void testSubmitText(){
		String text = "This course is bloody good";
		FormElements formElementPage = PageFactory.initElements(driver, FormElements.class);
		formElementPage.textArea.clear();
		formElementPage.textArea.sendKeys(text);
		formElementPage.submitButton.click();
		ProcessedPage processedPage = PageFactory.initElements(driver, ProcessedPage.class);
		assertEquals(text,processedPage.comments.getText());
		
	}
	@Test
	public void testSubmitRadio2(){
		
		FormElements formElementPage = PageFactory.initElements(driver, FormElements.class);
		assertTrue("Radio2 is not selected",formElementPage.rd2Button.isSelected());
		formElementPage.submitButton.click();
		ProcessedPage processedPage = PageFactory.initElements(driver, ProcessedPage.class);
		assertEquals("Radio2 is not selected","rd2",processedPage.rd2.getText());
		
	}
	@Test
	public void testSubmitCheckbox1Only(){
		
		FormElements formElementPage = PageFactory.initElements(driver, FormElements.class);
		for(WebElement cb: formElementPage.checkboxes){
				if(cb.isSelected()){cb.click();}
		}
		formElementPage.checkboxes.get(0).click();
		formElementPage.submitButton.click();
		ProcessedPage processedPage = PageFactory.initElements(driver, ProcessedPage.class);
		assertEquals("Checkbox1 is not selected","cb1",processedPage.cb1.getText());	
	}
	@Test
	public void testSubmitSingleDropdown(){
		
		FormElements formElementPage = PageFactory.initElements(driver, FormElements.class);
		Select singleDropdown = new Select(formElementPage.singleDropdown);
		if(singleDropdown.isMultiple()){System.out.println("This is a multiple select.");}
		else{singleDropdown.selectByIndex(4);}
		formElementPage.submitButton.click();
		ProcessedPage processedPage = PageFactory.initElements(driver, ProcessedPage.class);
		assertEquals("Item 5 is not selected","dd5",processedPage.singledropdown.getText());	
	}
	@Test
	public void testSubmitMultipleSelection(){
		
		FormElements formElementPage = PageFactory.initElements(driver, FormElements.class);
		Select multipleDropdown = new Select(formElementPage.multipleSelect);
		Actions builder = new Actions(driver);
		if(!multipleDropdown.isMultiple()){System.out.println("This is not a multiple select.");}
		else{
			multipleDropdown.deselectAll();
			builder.keyDown(Keys.CONTROL);
			multipleDropdown.selectByIndex(0);
			multipleDropdown.selectByIndex(1);
			multipleDropdown.selectByIndex(2);
			}
		formElementPage.submitButton.click();
		ProcessedPage processedPage = PageFactory.initElements(driver, ProcessedPage.class);
		for(int i=0;i<3;i++){
			
			assertEquals("You might miss something", "ms"+(i+1),processedPage.multipleSelect.get(i).getText());
		}	
	}
	@Test
	public void testUploadFile() throws URISyntaxException {
		
		FormElements formElementPage = PageFactory.initElements(driver, FormElements.class);
		File testFile = new File(this.getClass().
                getResource("/manipulation/New Text Document.txt").toURI());

		formElementPage.browseButton.sendKeys(testFile.getAbsolutePath());

		formElementPage.submitButton.click();
		//new WebDriverWait(driver,10).until(ExpectedConditions.titleIs("Processed Form Details"));
		ProcessedPage processedPage = PageFactory.initElements(driver, ProcessedPage.class);
		assertEquals("File name is not as expected.","New Text Document.txt",processedPage.filename.getText());	
	}
	
}
